

$(document).ready(function() {

	// 会员编辑确认
	$("#memberOK").click(function() {
		
		if ($("#imagePath_member").val() == "") {
			alertError("头像不能为空！");
			return false;
		}
		if ($("#fingerImg_member").attr("src") != base + "commonCss/images/zhiwen_c.png") {
			alertError("指纹必须采集！");
			return false;
		}
		if ($("#memName_member").val() == "") {
			alertError("姓名不能为空！");
			return false;
		}
		if ($("#girlRadioButton_member")[0].checked == false && $("#boyRadioButton_member")[0].checked == false) {
			alertError("性别不能为空！");
			return false;
		}
		if ($("#birthday_member").val() == "") {
			alertError("生日不能为空！");
			return false;
		}
		if ($("#country_member").val() == "") {
			alertError("国籍不能为空！");
			return false;
		}
		if (($("#pwd1_member").val() != "" || $("#pwd2_member").val() != "") && $("#pwd1_member").val() != $("#pwd2_member").val() != "") {
			alertError("接领密码必须相同！");
			return false;
		}
		if ($("#fkSales_member").val() == "") {
			alertError("销售不能为空！");
			return false;
		}
		$.messager.confirm('提示', '确认要保存吗?', function(r) {
			if (r) {

				$("#fing1_member").val($("#fing1").val());
				$("#fing2_member").val($("#fing2").val());
				$("#ftype1_member").val($("#ftype1").val());
				$("#ftype2_member").val($("#ftype2").val());
				
				$.ajax({
					cache : false,
					type : "post",
					dataType : "json",
					url : base + "membership/single/saveMember.shtml",
					data : $("#memberForm").serialize(),
					error : function(XMLHttpRequest) {
						ajaxError(XMLHttpRequest);
					},
					success : function(data) {
						if (data != null) {
							
							if (data.result == "1") {
								if (data.familyId != null) {
									// 首次添加会员，生成新的familyId
									familyId = data.familyId;
									$("#fkMemId_gua").val(data.memberId);
								}
								//alertInfo("保存成功");
								$('#memberPop').window('close');
								window.location.href = base + "membership/visitor/queryMemberDetail.shtml?memberId=" + data.memberId;
							} else if (data.result == "0") {
								alertError(data.message);
							}
						} else {
							alertError("保存失败！");
						}
					}
				});

			}
		});
	});

	// 非会员编辑确认
	$("#notMemberOK").click(function() {

		if ($("#imagePath_notMember").val() == "") {
			alertError("头像不能为空！");
			return false;
		}
		if ($("#fingerImg_notMember").attr("src") != base + "commonCss/images/zhiwen_c.png") {
			alertError("指纹必须采集！");
			return false;
		}
		if ($("#memName").val() == "") {
			alertError("姓名不能为空！");
			return false;
		}
		if ($("#girlRadioButton")[0].checked == false && $("#boyRadioButton")[0].checked == false) {
			alertError("性别不能为空！");
			return false;
		}
		if ($("#birthday").val() == "") {
			alertError("生日不能为空！");
			return false;
		}
		if ($("#country").val() == "") {
			alertError("国籍不能为空！");
			return false;
		}
		if (($("#pwd1").val() != "" || $("#pwd2").val() != "") && $("#pwd1").val() != $("#pwd2").val() != "") {
			alertError("接领密码必须相同！");
			return false;
		}
		if ($("#memType").val() == "3" && $("#fkSales").val() == "") {
			alertError("销售不能为空！");
			return false;
		}

		$.messager.confirm('提示', '确认要保存吗?', function(r) {
			if (r) {
				$("#fing1_notMember").val($("#fing1").val());
				$("#fing2_notMember").val($("#fing2").val());
				$("#ftype1_notMember").val($("#ftype1").val());
				$("#ftype2_notMember").val($("#ftype2").val());
				$.ajax({
					cache : false,
					type : "post",
					dataType : "json",
					url : base + "membership/single/saveMember.shtml",
					data : $("#notMemberForm").serialize(),
					error : function(XMLHttpRequest) {
						ajaxError(XMLHttpRequest);
					},
					success : function(data) {
						if (data != null) {
							
							if (data.result == "1") {
								if (data.familyId != null) {
									// 首次添加会员，生成新的familyId
									familyId = data.familyId;
									$("#fkMemId_gua").val(data.memberId);
								}
								//alertInfo("保存成功");
								$('#notMemberPop').window('close');
								window.location.href = base + "membership/visitor/queryMemberDetail.shtml?memberId=" + data.memberId;
							} else if (data.result == "0") {
								alertError(data.message);
							}
						} else {
							alertError("保存失败！");
						}
					}
				});

			}
		});
	});

	// 监护人编辑确认
	$("#guaOK").click(function() {

		if ($("#imagePath_gua").val() == "") {
			alertError("头像不能为空！");
			return false;
		}
		if ($("#name").val() == "") {
			alertError("姓名不能为空！");
			return false;
		}
		if ($("#telNum").val() == "") {
			alertError("号码不能为空！");
			return false;
		}
		if (!/\d+/.test($("#telNum").val())) {
			alertError("号码只能为数字！");
			return false;
		}

		$.messager.confirm('提示','确认要保存吗?',function(r) {
			if (r) {
				$("#fing1_gua").val($("#fing1").val());
				$("#fing2_gua").val($("#fing2").val());
				$("#ftype1_gua").val($("#ftype1").val());
				$("#ftype2_gua").val($("#ftype2").val());
				$.ajax({
					cache : false,
					type : "post",
					dataType : "json",
					url : base + "membership/single/saveGua.shtml?country=" + $("#country_gua").val(),
					data : $("#guaForm").serialize(),
					error : function(XMLHttpRequest) {
						ajaxError(XMLHttpRequest);
					},
					success : function(data) {
						if (data != null) {
							if (data.result == "1") {
								alertInfo("保存成功");
								$('#notMemberPop').window('close');
								window.location.reload();
							} else if (data.result == "0") {
								alertError(data.message);
							}
						} else {
							alertError("保存失败！");
						}
					}
				});

			}
		});
	});

});

//打开编辑会员
function openMember(memberId) {
	$("#imagePath_member").val();
	$("#fing1_member").val("");
	$("#fing2_member").val("");
	$.ajax({
		cache : false,
		type : "get",
		dataType : "json",
		url : base + "membership/single/queryMemberById.shtml",
		data : "memberId=" + memberId,
		error : function(XMLHttpRequest) {
			ajaxError(XMLHttpRequest);
		},
		success : function(data) {
			if (data == null) {
				alertInfo("没有匹配的会员!");
			} else {
				$("#pkMemId_member").val(data.pkMemId);
				if (data.imagePath == null) {
					$("#memberImage").attr("src",base + "commonCss/images/index_03.gif");
				} else {
					$("#imagePath_member").val(data.imagePath);
					$("#memberImage").attr("src",base + "common/showImage.shtml?imagePath=" + data.imagePath);
				}
				$("#memName_member").val(data.memName);
				$("#memberCardName").html(data.memberCardName);
				if (data.sex == "男") {
					$("#boyRadioButton_member").attr("checked", true);
				} else {
					$("#girlRadioButton_member").attr("checked", true);
				}
				if (data.isVip == "1") {
					$("#isVip_member").attr("checked", true);
				} else {
					$("#isVip_member").attr("checked", false);
				}
				$("#birthday_member").val(data.birthdayPage.replace(/\./g, "-"));
				$("#country_member").val(data.country);
				$("#spanMemberId_member").html(data.pkMemId);
				if (data.canConsume == "1") {
					$("#canConsume_member").attr("checked", true);
				} else {
					$("#canConsume_member").attr("checked", false);
				}
				$("#pwd1_member").val(data.receivePwd);
				$("#pwd2_member").val(data.receivePwd);
				if (data.signDisclaimer == "1") {
					$("#signDisclaimer_member").attr("checked", true);
				} else {
					$("#signDisclaimer_member").attr("checked", false);
				}
				$("#note").val(data.note);
				$("#memo").val(data.memo);
				$("#fkSales_member").val(data.fkSales);
				$("#fing1").val(data.fing1);
				$("#fing2").val(data.fing2);
				if (data.fing1 != null || data.fing2 != null) {
					$("#fingerImg_member").attr("src", base + "commonCss/images/zhiwen_c.png");
				} else {
					$("#fingerImg_member").attr("src", base + "commonCss/images/zhiwen_w.png");
				}
				refreshSelect();

				$('#memberPop').window('open');
			}
			return;
		}
	});
}

// 打开非会员
function openNotMember(memberId) {
	$("#imagePath_notMember").val();
	$("#fing1_notMember").val("");
	$("#fing2_notMember").val("");
	if (memberId == "") {
		$("#pkMemId").val("");
		$("#notMemberImage").attr("src", base + "commonCss/images/index_03.gif");
		$("#memName").val("");
		$("#memType").val("3");
		$("#girlRadioButton").attr("checked", false);
		$("#boyRadioButton").attr("checked", false);
		$("#isVip").attr("checked", false);
		$("#birthday").val("");
		$("#country").val("");
		$("#pk").hide();
		$("#spanMemberId").html("");
		$("#canConsume").attr("checked", false);
		$("#pwd1").val("");
		$("#pwd2").val("");
		$("#signDisclaimer").attr("checked", false);
		$("#fkSales").val("");
		refreshSelect();
		$("#fing1").val("");
		$("#fing2").val("");
		$("#fingerImg_notMember").attr("src", base + "commonCss/images/zhiwen_w.png");
		$("#notMemberPop").attr("title", "新增");
	} else {
		$.ajax({
			cache : false,
			type : "get",
			dataType : "json",
			url : base + "membership/single/queryMemberById.shtml",
			data : "memberId=" + memberId,
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			},
			success : function(data) {
				if (data == null) {
					alertInfo("没有匹配的会员!");
				} else {
					$("#pkMemId").val(data.pkMemId);
					if (data.imagePath == null) {
						$("#notMemberImage").attr("src", base + "commonCss/images/index_03.gif");
					} else {
						$("#imagePath_notMember").val(data.imagePath);
						$("#notMemberImage").attr("src",base + "common/showImage.shtml?imagePath=" + data.imagePath);
					}
					$("#memName").val(data.memName);
					$("#memType").val(data.memType);
					if (data.sex == "男") {
						$("#boyRadioButton").attr("checked", true);
					} else {
						$("#girlRadioButton").attr("checked", true);
					}
					if (data.isVip == "1") {
						$("#isVip").attr("checked", true);
					} else {
						$("#isVip").attr("checked", false);
					}
					$("#birthday").val(data.birthdayPage.replace(/\./g, "-"));
					$("#country").val(data.country);
					$("#pk").show();
					$("#spanMemberId").html(data.pkMemId);
					if (data.canConsume == "1") {
						$("#canConsume").attr("checked", true);
					} else {
						$("#canConsume").attr("checked", false);
					}
					$("#pwd1").val(data.receivePwd);
					$("#pwd2").val(data.receivePwd);
					if (data.signDisclaimer == "1") {
						$("#signDisclaimer").attr("checked", true);
					} else {
						$("#signDisclaimer").attr("checked", false);
					}
					$("#fkSales").val(data.fkSales);
					$("#fing1").val(data.fing1);
					$("#fing2").val(data.fing2);
					if (data.fing1 != null || data.fing2 != null) {
						$("#fingerImg_notMember").attr("src", base + "commonCss/images/zhiwen_c.png");
					} else {
						$("#fingerImg_notMember").attr("src", base + "commonCss/images/zhiwen_w.png");
					}
					refreshSelect();
				}
				return;
			}
		});
		$("#notMemberPop").attr("title", "编辑");
	}
	$('#notMemberPop').window('open');
}

// 打开新增/编辑监护人
function openGua(guaId) {
	if (familyId == "") {
		alertError("请先添加会员信息！");
		return false;
	}
	$("#imagePath_gua").val("");
	$("#fing1_gua").val("");
	$("#fing2_gua").val("");
	if (guaId == "") {
		$("#pkGuaId").val("");
		$("#imagePath_gua").val("");
		$("#guaImage").attr("src", base + "commonCss/images/index_02.gif");
		$("#fkReltypeId").val("");
		refreshSelect();
		$("#name").val("");
		$("#telNum").val("");
		$("#email").val("");
		$("#canConsume").attr("checked", false);
		$("#canReceive").attr("checked", false);
		$("#fing1").val("");
		$("#fing2").val("");
		$("#fingerImg_gua").attr("src", base + "commonCss/images/zhiwen_w.png");
		$("#guaPop").attr("title", "新增");
	} else {
		$.ajax({
			cache : false,
			type : "get",
			dataType : "json",
			url : base + "membership/single/queryGuaById.shtml",
			data : "guaId=" + guaId,
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			},
			success : function(data) {
				if (data == null) {
					alertInfo("没有匹配的监护人!");
				} else {
					$("#pkGuaId").val(data.pkGuaId);
					if (data.imagePath == null) {
						$("#guaImage").attr("src", base + "commonCss/images/index_02.gif");
					} else {
						$("#imagePath_gua").val(data.imagePath);
						$("#guaImage").attr("src", base + "common/showImage.shtml?imagePath=" + data.imagePath);
					}
					$("#fkReltypeId").val(data.fkReltypeId);
					refreshSelect();
					$("#name").val(data.name);
					$("#telNum").val(data.telNum);
					$("#email").val(data.email);
					if (data.canConsume == "1") {
						$("#canConsume_gua").attr("checked", true);
					} else {
						$("#canConsume_gua").attr("checked", false);
					}
					if (data.canReceive == "1") {
						$("#canReceive").attr("checked", true);
					} else {
						$("#canReceive").attr("checked", false);
					}
					$("#fing1").val(data.fing1);
					$("#fing2").val(data.fing2);
					if (data.fing1 != null || data.fing2 != null) {
						$("#fingerImg_gua").attr("src", base + "commonCss/images/zhiwen_c.png");
					} else {
						$("#fingerImg_gua").attr("src", base + "commonCss/images/zhiwen_w.png");
					}
				}
				return;
			}
		});
		$("#guaPop").attr("title", "编辑");
	}
	$('#guaPop').window('open');
}

// 删除会员
function deleteMember(memberId) {
	$.messager.confirm('提示','确认要删除吗?',function(r) {
		if (r) {
			$.ajax({
				cache : false,
				type : "get",
				dataType : "json",
				url : base + "membership/single/deleteMember.shtml",
				data : "memberId=" + memberId,
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				},
				success : function(data) {
					if (data == 1) {
						$("#li_member_" + memberId).remove();
					}
				}
			});
		}
	});
}

// 删除监护人
function deleteGua(guaId) {
	$.messager.confirm('提示','确认要删除吗?',function(r) {
		if (r) {
			$.ajax({
				cache : false,
				type : "get",
				dataType : "json",
				url : base + "membership/single/deleteGua.shtml",
				data : "guaId=" + guaId,
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				},
				success : function(data) {
					if (data == 1) {
						$("#li_gua_" + guaId).remove();
					}
				}
			});
		}
	});
}


function openCameraPage(flag){
	var returnValue=window.showModalDialog(base + "filemanagement/openCamera.shtml",window,"height=500,width=611,scrollbars=yes,status =yes");
	if (returnValue==1) {
	        //发送ajax请求获取上传成功的图片名称
		$.ajax({
		        type: 'get',  
		        url: base + "filemanagement/getUploadedImageName.shtml",
		        dataType:"json",
		        cache: false,
		        async: false,
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			},
		        success: function(data) {
		        	var imageName=data.imageName;
		        	var imageSrc=base + "common/showImage.shtml?imagePath="+imageName;
		        	switch (flag) {
					case 1:
						//创建会员
						$("#memberImage").attr("src",imageSrc);
						$("#imagePath_member").val(imageName);
						break;
					case 2:
						//编辑会员
						$("#notMemberImage").attr("src",imageSrc);
						$("#imagePath_notMember").val(imageName);
						break;
					case 3:
						//编辑监护人
						$("#guaImage").attr("src",imageSrc);
						$("#imagePath_gua").val(imageName);
						break;
				}
	    	        }
		});  
		        
	}
}

// 打开指纹扫描界面
function openFingerPrint() {
	if ($("#fing1").val() == "") {
		$("#imgFing1").attr("src", base + "commonCss/images/zhiwen_w.png");
	} else {
		$("#imgFing1").attr("src", base + "commonCss/images/zhiwen_c.png");
	}
	if ($("#fing2").val() == "") {
		$("#imgFing2").attr("src", base + "commonCss/images/zhiwen_w.png");
	} else {
		$("#imgFing2").attr("src", base + "commonCss/images/zhiwen_c.png");
	}
	$('#fingerprintPop').window('open');
}

// 指纹类型 1：会员 2：非会员 3：监护人
var fingerType;
var fingerFlag;

/**
 * 采集指纹特征
 * @param flag
 * 	1 ：主手指
 * 	2 ：副手指
 */
function collectFingerFea(flag) {
	
	fingerData = null;
	fingerFlag = null;
	
	try {
		var data = collectFingerTemplate();
		if (data != "") {
			var exist = false;
			$.ajax({
				cache : false,
				async: false,
				type : "post",
				dataType : "json",
				url : base + "membership/single/checkFingerprintExist.shtml",
				data : "fingerprint=" + encodeURIComponent(data),
				error : function(XMLHttpRequest) {
					ajaxError(XMLHttpRequest);
				},
				success : function(data) {
					if (data == 0) {
						alertError("指纹已存在于其它用户，不可重复创建！");
						exist = true;
						return;
					}
				}
			});
			if (exist) return;
			
			$("#fing" + flag).val(data);
			$("#imgFing" + flag).attr("src", base + "commonCss/images/zhiwen_c.png");
			fingerFlag = flag;
			$('#fingerPop').window('open');
		}
	} catch (e) {
		return "";
	}
}

function fingerprintOK() {
	if ($("#ftype1").val() != "" || $("#ftype2").val() != "") {
		$("#fingerImg_" + fingerType).attr("src", base + "commonCss/images/zhiwen_c.png");
	}
	$('#fingerprintPop').window('close');
}

function fingerSelectOK() {
	if ($("input[name='fingerIndex']:checked").val() == null) {
		alertError("请选择手指！");
		return false;
	}
	$("#ftype" + fingerFlag).val($("input[name='fingerIndex']:checked").val());
	$('#fingerPop').window('close');
}

function fingerSelectCancel() {
	$("#fing" + fingerFlag).val("");
	$("#ftype" + fingerFlag).val("");
	$("#fingerImg_" + fingerType).attr("src", base + "commonCss/images/zhiwen_w.png");
	$('#fingerPop').window('close');
}
